﻿using UnityEngine;
using System.Collections;

public class GUIbar : MonoBehaviour 
{
	Texture2D barBackColor;			//this is the backing color that will show behind the filler
	Texture2D barFillColor;			//this will be the color of the adjustable bar in the centre showing how full this item is
	Texture2D barTypeSymbol;			//this image will show a representation of what gui bar type this is (i.e red plus for health bar)
	
	Vector2 screenPosition;			//this will offset the gui bar from the top left corner
	float boarderThickness;			//this needs to be limited between 0 and 1

	float barHeight;					//how tall the bar is - this is a percentage between 0-100 of screen height
	float barWidth;					//this will be how long the actual bar that will be filled is - this is a percentage between 0-100 of screen width

	public bool isEnabled = true;
	float fillPercent = 100.0f;



	public void SetBarPercent(float percent)
	{
		if(percent < 0 || percent > 100)
		{
			Debug.Log("ERROR: can only fill bar between 0 - 100%");
			Mathf.Clamp(percent,0.0f,100.0f);
		}
		fillPercent = percent;
	}

	public void SetSymbolTexture(Texture2D tex) { barTypeSymbol = tex; }
	public void SetBoarderTexture(Texture2D tex) { barBackColor = tex; }
	public void SetFillTexture(Texture2D tex) { barFillColor = tex; }
	public void SetPosition(Vector2 pos) { screenPosition = pos; }
	public void SetBoarderTickness(float thickness)
	{
		//boarder thickness is between 0-1 and this will clamp it to make sure
		if(thickness <0 || thickness > 1)
		{
			Debug.Log("ERROR: boarderThickness should be between 0 and 1");
			Mathf.Clamp01(thickness);
		}
		
		boarderThickness = thickness;
	}

	//set up any values that have not been pre set
	void Start()
	{
		//set default textures if not filled already
		if(barBackColor == null)
		{

			barBackColor = new Texture2D (1, 1);
			barBackColor.SetPixel(0,0,Color.black);
			barBackColor.Apply();
		}

		if(barFillColor == null)
		{
			barFillColor = new Texture2D (1, 1);
			barFillColor.SetPixel(0,0,Color.green);
			barFillColor.Apply();
		}

		if(barTypeSymbol == null)
		{
			barTypeSymbol = new Texture2D (1, 1);
			barTypeSymbol.SetPixel(0,0,Color.red);
			barTypeSymbol.Apply();
		}

		//set Default Location
		if(screenPosition == Vector2.zero)
		{
			screenPosition = new Vector2 (2, 2);
		}

		
		//Set default sizes
		if(boarderThickness == 0)
			SetBoarderTickness (0.1f);
		if(barHeight == 0)
			barHeight = 6.0f;
		if(barWidth == 0)
			barWidth = 15.0f;
	}
	
	//This function will display the bar on screen
	void OnGUI () 
	{
		//boarder thickness is between 0-1 and this will clamp it to make sure
		if(boarderThickness <0 || boarderThickness > 1)
		{
			Debug.Log("ERROR: boarderThickness should be between 0 and 1");
			Mathf.Clamp01(boarderThickness);
		}
		
		//these numbers make it easier to adjust the values from the inspector. This way the user can change between 0-100 instead of 0-1
		float sWidth = Screen.width / 100.0f;
		float sHeight = Screen.height / 100.0f;
		
		//make variables to simplify the rect creation
		float xOffset = screenPosition.x * sWidth;
		float yOffset = screenPosition.y * sHeight;
		float symbolSize = barHeight * sHeight;
		float outerBarWidth = barWidth * sWidth;
		float fillerBoarder = boarderThickness * symbolSize * 0.5f;
		
		//make rects to draw to
		Rect symbolRect = new Rect (xOffset, yOffset, symbolSize , symbolSize);
		Rect frameRect = new Rect (xOffset + symbolSize + fillerBoarder, yOffset , outerBarWidth, symbolSize );
		Rect fillRect = new Rect(xOffset + symbolSize + 2 * fillerBoarder , yOffset + fillerBoarder , (outerBarWidth - 2 * fillerBoarder) * (fillPercent/100.0f) , (symbolSize - 2 * fillerBoarder));
		
		//draw the bar to the screen
		GUI.DrawTexture( symbolRect, barTypeSymbol);
		GUI.DrawTexture( frameRect, barBackColor);
		GUI.DrawTexture( fillRect, barFillColor);
		
	}
}
